from flask import Blueprint, request
from components import resDTO
from question import ChatBot

questionBlueprint = Blueprint('question', __name__, url_prefix='/graph')

# 初始化问答对象
chatOBJ = ChatBot()


@questionBlueprint.route('/chat', methods=['POST'])
def askQuestion():
    """
        问询接口，可能返回多个答案
        ---
        tags:
          - 图问询接口
        description:
            图问询接口，json格式
        parameters:
          - name: question
            in: body
            description: 问题描述
            required: true
            schema:
              type: object
              properties:
                question:
                  type: string
                is_chat_kg:
                  type: boolean
                  default: false
        responses:
          200:
            description: 响应成功
        """
    data = request.json.get('question')
    is_chat_kg = request.json.get('is_chat_kg', False)  # 获取参数，默认false
    answer = chatOBJ.chat_main(data, is_chat_kg=is_chat_kg)  # 传递参数
    result = {'answer': answer}
    return resDTO(data=result)


@questionBlueprint.route('/chatListToSolution', methods=['POST'])
def askQuestionTwo():
    """
        问询接口，根据诊断结论，同时给出故障原因和维修建议
        ---
        tags:
          - 图问询接口
        description:
            图问询接口，json格式\n
            以下是问题示例：\n
            存在油液缺失，注油嘴缺失或被堵塞\n
            连接螺栓出现了松动、断裂或缺失情况，连接筋板有开焊现象\n
        parameters:
          - name: question
            in: body
            description: 问题描述
            required: true
            schema:
              id: question
              type: object
              properties:
                question_list:
                  type: array
                  items:
                    schema:
                      id: question_item
                      type: string
        examples:
          rgb: ['red', 'green', 'blue']
        responses:
          200:
            description: 响应成功
        """
    questions = request.json.get('question_list')
    res = []
    for question in questions:
        answer1 = chatOBJ.chat_main("根据以下诊断结论，给出故障原因：\n" + question)
        answer2 = chatOBJ.chat_main("根据以下诊断结论，给出维修方案：\n" + question)
        result = {'cause': answer1, 'solution': answer2}
        res.append(result)
    return resDTO(data=res)


@questionBlueprint.route('/chatToSolution', methods=['POST'])
def askListQuestionTwo():
    """
        问询接口，根据诊断结论，同时给出故障原因和维修建议
        ---
        tags:
          - 图问询接口
        description:
            图问询接口，json格式\n
            以下是问题示例：\n
            存在油液缺失，注油嘴缺失或被堵塞\n
            连接螺栓出现了松动、断裂或缺失情况，连接筋板有开焊现象\n
        parameters:
          - name: question
            in: body
            description: 问题描述
            required: true
            schema:
              type: object
              properties:
                question:
                  type: string
        responses:
          200:
            description: 响应成功
        """
    question = request.json.get('question')
    answer1 = chatOBJ.chat_main("根据以下诊断结论，给出故障原因：\n" + question)
    answer2 = chatOBJ.chat_main("根据以下诊断结论，给出维修方案：\n" + question)
    result = {'cause': answer1, 'solution': answer2}
    return resDTO(data=result)
